

  class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

class Solution {

    public int preIndex;
    public TreeNode buildTree(int[] preorder, int[] inorder) {

        TreeNode root=creatTree(preorder,inorder,0,preorder.length-1);
        return root;


    }

    private TreeNode creatTree(int[] preorder, int[] inorder, int s, int e) {

        if(s>e){
            return null;
        }


        int rootIndex=finVal(inorder,preorder[preIndex],s,e);
        TreeNode root=new TreeNode(preorder[preIndex]);
        preIndex++;

        root.left=creatTree(preorder,inorder,s,rootIndex-1);
        root.right=creatTree(preorder,inorder,rootIndex+1,e);




        return root;

    }

    private int finVal(int[] inorder, int i,int s,int e) {
        for (int j = s; j <=e; j++) {
            if(inorder[j]==i){
                return j;
            }
        }

        return -1;


    }
}